<?php


namespace App\Http\Services;


use App\Exceptions\ErrorException;
use App\Http\Common\Constant;
use App\Models\CompanyIncome;
use App\Models\Member;
use App\Models\MemberRecommend;
use App\Models\Staff;
use App\Models\StaffTotalIncome;
use App\Models\Store;
use App\Models\StoreRecommendMember;
use Carbon\Carbon;
use DB;
use Exception;
use Log;

class CardOrderService
{

    /**
     * 分配
     */
    public function distribution($order)
    {
        //写入用户数据
        $time = Carbon::now();
//        try {
//            DB::beginTransaction();
        $member = Member::find($order->member_id);
        if ($member == null) {
            Log::error('用户错误');
            DB::rollBack();
            throw new ErrorException('用户不存在');
        }
        $total = Constant::CARD_MONEY;
        $member->increment('card_money', $order->money - $total);//余额
        $member->increment('card_money_coupon', $total);//卡券

        $platformTotal = $total * Constant::CARD_PLATFORM_INCOME;

        $storeTotal = $total * Constant::CARD_STORE_INCOME;

        $storeReTotal = $total * Constant::CARD_STORE_RE_INCOME;
        $memberReTotal = $total * Constant::CARD_MEMBER_RE_INCOME;

        if ($total != ($platformTotal + $storeReTotal + $storeTotal + $memberReTotal)) {
            Log::debug('购卡资金分配错误!');
            DB::rollBack();
            throw new ErrorException('错误');
        }
        //公司
        $company = CompanyIncome::first();
        $companyTotal = $platformTotal * Constant::CARD_PLATFORM_COMPANY;
        $company->increment('total_money', $companyTotal);
        $company->increment('usable_money', $companyTotal);
        DB::table('company_card_income_log')->insert([
            'member_recharge_card_order_id' => $order->id,
            'money' => $companyTotal,
            'proportion' => Constant::CARD_PLATFORM_COMPANY,
            'created_at' => $time
        ]);
        //员工总
        $staffTotal = $platformTotal * Constant::CARD_PLATFORM_STAFF;
        $staff = StaffTotalIncome::first();
        $staff->increment('total_money', $staffTotal);
        $staff->increment('useable_money', $staffTotal);
        DB::table('staff_card_income_log')->insert([
            'member_recharge_card_order_id' => $order->id,
            'money' => $staffTotal,
            'proportion' => Constant::CARD_PLATFORM_STAFF,
            'created_at' => $time
        ]);
        //细分员工
        $staffCount = Staff::count();
        if ($staffCount > 0) {
            $staffSingle = floor($staffTotal / $staffCount);
            Staff::increment('total_money', $staffSingle);
            Staff::increment('useable_money', $staffSingle);
        }
        //商家
        $storeR = StoreRecommendMember::where('member_id', $order->member_id)->first();
        if ($storeR) {
            $store = Store::find($storeR->store_id);
            $store->increment('total_money', $storeTotal);
            $store->increment('usable_money', $storeTotal);
            DB::table('store_card_income_log')->insert([
                'member_recharge_card_order_id' => $order->id,
                'money' => $storeTotal,
                'proportion' => Constant::CARD_STORE_INCOME,
                'created_at' => $time
            ]);
        }
        //商家的推荐人
        $storeRe = DB::table('member_recommend_store')->where('store_id', $order->store_id)->first();
        if ($storeRe) {
            $memberReM = Member::find($storeR->member_id);
            $memberReM->increment('useable_share_money', $memberReTotal);
            $memberReM->increment('total_share_money', $memberReTotal);
            DB::table('member_recommend_store_card_income_log')->insert([
                'member_recharge_card_order_id' => $order->id,
                'money' => $storeReTotal,
                'proportion' => Constant::CARD_STORE_RE_INCOME,
                'created_at' => $time
            ]);
        }
        //会员的推荐人
        $memberRe = MemberRecommend::where('recommend_id', $order->member_id)->first();
        if ($memberRe) {
            $memberReM = Member::find($memberRe->member_id);
            $memberReM->increment('useable_share_money', $memberReTotal);
            $memberReM->increment('total_share_money', $memberReTotal);
            DB::table('member_card_income_log')->insert([
                'member_recharge_card_order_id' => $order->id,
                'member_id' => $memberReM->id,
                'money' => $memberReTotal,
                'proportion' => Constant::CARD_MEMBER_RE_INCOME,
                'created_at' => $time
            ]);
        }
//            DB::commit();
//        } catch (Exception $e) {
////            DB::rollBack();
//            Log::error($e);
//            throw new ErrorException('错误');
//        }
        return true;
    }
}